<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Area;
use App\Model\Areapoint;

class GridController extends Controller
{
	// index方法
    public function index()
    {
        $tree = array();
    	$area_first = array();
    	$area_first = Area::where('name','北宅街道')->first()->toArray();
        $tree = $area_first;
    	$where = array();
        $where[] = array('pid', $area_first['id']);
    	$area = Area::where($where)->select(array('id','pid','node','name','level'))->get()->toArray();
    	$tree['street'] = $area;
    	return view('admin.grid.index',[
    		'tree' => $tree
    	]);
    }

    public function getAllPoints()
    {
    	$points = Areapoint::get();
    	$points = json_decode(json_encode($points), true);

    	$area_first = array();
    	$area_first = Area::where('name','青岛市')->first()->toArray();
    	$parent_path = $area_first['node'].','.$area_first['id'];
    	$where = array();
    	$where[] = array('node', 'like', $parent_path.'%');
    	$area = Area::where($where)->select(array('id', 'name'))->get()->toArray();
    	$area = array_column($area, null, 'id');

    	$returnData = array();
    	$returnData = $points;
    	foreach($returnData as $k=>$v) {
    		$returnData[$k]['area_name'] = $area[$v['area_id']]['name'];
    	}

    	return response()->json($returnData);
    }

    public function getPointsNum(Request $request)
    {
    	$community_id = $request->input("id");
    	$points_num = Areapoint::where('area_id', $community_id)->count();

    	$returnData = array();
    	$returnData['points_num'] = $points_num;

    	return response()->json($returnData);

    }

    public function addPoints(Request $request)
    {
    	$data = $request->all();
    	$area_id = $data['area_id'];
    	$area = Areapoint::where('area_id', $area_id)->first();
    	$area_arr = json_decode(json_encode($area), true);
    	if(!empty($area_arr)) {
    		$area->color= $data['color'];
    		$area->lng_lat = $data['lng_lat'];
    		$res = $area->save();
    	} else {
    		$res = Areapoint::insert($data);
    	}

    	$returnData = array();
    	if($res !== false) {
    		$returnData['status'] = 1;
    	} else {
    		$returnData['status'] = 0;
    	}
    	return response()->json($returnData);

    }

    public function deletePoints(Request $request)
    {
    	$area_id = $request->input("area_id");
    	$area_points = Areapoint::where('area_id', $area_id)->get();
    	$area_points = json_decode(json_encode($area_points), true);
    	$returnData = array();
    	$returnData['status'] = 1;
    	if(!empty($area_points)) {
    		$res = Areapoint::where('area_id', $area_id)->delete();
    		if($res==false) {
    			$returnData['status'] = 0;
    		}
    	}

    	return response()->json($returnData);
	}
	
	public function getChildPoints(Request $request)
	{
		$pid = $request->input("area_id");
		$where = array();
		$where[] = array('pid', $pid);
		$area = Area::where($where)->select(array('id','pid','node','name','level'))->get();
		$area = json_decode(json_encode($area), true);
		
		return response()->json($area);
	}
}
